package com.czy.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author chenzy
 * @since 2020-04-08
 */
public class MyList<T> extends ArrayList<T> {
    public MyList(int initialCapacity) {
        super(initialCapacity);
    }

    public MyList() {
    }

    public MyList(List<T> valueList) {
        super(valueList);
    }

    public MyList<T> addValue(int index, T value) {
        this.add(index, value);
        return this;
    }

    public MyList<T> addValue(int index, List<T> valueList) {
        this.addAll(index, valueList);
        return this;
    }

    public MyList<T> setValue(int index, List<T> valueList) {
        for (var value : valueList) {
            this.set(index++, value);
        }
        return this;
    }
    public MyList<T> setValue(int index, T value) {
        this.set(index, value);
        return this;
    }


    public MyList<T> addValue(T value) {
        this.add(value);
        return this;
    }

    @SafeVarargs
    public final MyList<T> addValue(T... values) {
        this.addAll(Arrays.asList(values));
        return this;
    }

    public MyList<T> addValue(List<T> valueList) {
        this.addAll(valueList);
        return this;
    }

    @SafeVarargs
    public static <T> MyList<T> of(T... values) {
        if (values == null) {
            return of();
        }
        var list = new MyList<T>(values.length);
        for (T t : values) {
            if (t != null) {
                list.add(t);
            }
        }
        return list;
    }

    public static <T> MyList<T> of() {
        return new MyList<>();
    }

    public static <T> MyList<T> of(T t) {
        if (t == null) {
            return of();
        }
        var list = new MyList<T>();
        list.add(t);
        return list;
    }

}
